<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JsUnit Product Tests</title>
<link type="text/css" rel="Stylesheet" media="screen" href="../../stylesheets/styles.css"/>
<script language="JavaScript" type="text/javascript" src="../app/jsUnitCore.js"></script>
<script src="../../dependencies/prototype.js" type="text/javascript"></script>
<script src="../../dependencies/inflection.js" type="text/javascript"></script>
<script src="../../resources.js" type="text/javascript"></script>

<script language="JavaScript" type="text/javascript">

var people;
var pelle;
var bob;
var change;
var regex="(\w+)_(\d+)";

var mock_request;

// Example class

var Person={
	initialize:function(){
		this.name=this.nameFunction();
		this.email=this.emailFunction();
	},
	nameFunction:function(){
		return this.get('name');
	},
	emailFunction:function(){
		return this.get('email');
	}
}

var Message={
	nameFunction:function(){
		return this.get('name');
	}
}

Ajax.Request.prototype.initialize=function(url, options) {
    this.setOptions(options);
	mock_request={options:this.options,url:url};
	if(this.options.onSuccess)
		this.options.onSuccess({});
}

Ajax.Updater.prototype.initialize=function(element, url, options) {
    this.setOptions(options);
	mock_request={element:element,options:this.options,url:url};
}

function setUp(){
	mock_request=null;
	people=$RES('people');
	pelle=people.find(1);
	bob=$REC('person_2');
}

function testMixedInMethods(){
	assertNotNull(pelle.initialize);
	assertNotNull(pelle.nameFunction);
	assertNotNull(pelle.emailFunction);
	
	assertEquals('Pelle',pelle.nameFunction());
	assertEquals('pelleb@gmail.com',pelle.emailFunction());

	assertNotNull(pelle.name);
	assertNotNull(pelle.email);
	assertEquals('Pelle',pelle.name);
	assertEquals('pelleb@gmail.com',pelle.email);
}
function textRecordWithoutMixedInMethods(){
	var team=$REC('team_1');
	assertNotNull(team);
}
function textRecordWithMixedInMethodsButNoConstructor(){
	var message=$REC('message_1');
	assertNotNull(message);
	assertEqual("We shall overcome",message.get('body'));
}

function testNamesFromShortcut(){
	assertEquals("person_2",bob.id);
	assertEquals("id",2,bob.db_id());
	assertEquals("instance_name","person",bob.instance_name());
	assertEquals("collection_name","people",bob.collection_name());
	assertEquals("module name","Person",bob.module_name())
}
function testNamesFromFind(){
	assertEquals("person_1",pelle.id);
	assertEquals("id",1,pelle.db_id());
	assertEquals("instance_name","person",pelle.instance_name());
	assertEquals("collection_name","people",pelle.collection_name());
	assertEquals("module name","Person",pelle.module_name())
}
function testAttributes(){
	assertEquals(1,pelle.db_id());
	assertEquals('Pelle',pelle.get('name'));
	assertEquals('pelleb@gmail.com',pelle.get('email'));

	assertEquals("person_2",bob.id);
	assertEquals(2,bob.db_id());
	assertEquals('Bob',bob.get('name'));
	assertEquals('bob@gmail.com',bob.get('email'));
	
}
function testGetElement(){
	assertNotNull(pelle.getElement('name'));
	assertTrue(pelle.getElement('name').hasClassName('name'));
	assertNotNull(pelle.getElement('email'));
	assertTrue(pelle.getElement('email').hasClassName('email'));
	
}

function testGetAttributeFromAbbr(){
	var message=$REC('message_1');
	assertNotNull(message);
	assertEquals("2007-05-01T16:45:07PDT",message.get('created_at'));
}
function testGetAttributeFromAbbrWithMissingTitle(){
	var message=$REC('message_2');
	assertNotNull(message);
	assertEquals("1 May 16:45",message.get('created_at'));
}


function testToHash(){
	var hash=pelle.toHash();
	assertNotNull(hash);
	assertEquals('Pelle',hash.name);
	assertEquals('pelleb@gmail.com',hash.email);
}
function testToHashWithAbbr(){
	var message=$REC('message_1');
	var hash=message.toHash();
	assertNotNull(hash);
	assertEquals('We shall overcome',hash.body);
	assertEquals('2007-05-01T16:45:07PDT',hash.created_at);
}

function testToQueryString(){
	var query=pelle.toQueryString();
	assertNotNull(query);
	assertEquals("name=Pelle&email=pelleb%40gmail.com",query);
}

function testSetAttribute(){
	change=$REC('person_4');
	assertNotNull(change);
	assertEquals("Change Me",change.get('name'));
	change.set('name','I changed you');
	assertEquals("I changed you",change.get('name'));
}

function testUpdateAttribute(){
	change=$REC('person_5');
	assertNotNull(change);
	assertEquals("Change Me",change.get('name'));
	change.update_attribute('name','I changed you');
	assertEquals("I changed you",change.get('name'));
	assertNotNull(mock_request);
	assertEquals('/people/5',mock_request.url);
	assertEquals('put',mock_request.options.method);
}
function testComplexHCard(){
	var vcard=$REC('contact_1');
	assertNotNull(vcard);
	assertEquals("Wikimedia Foundation Inc.",vcard.get('fn'));
	assertEquals("Wikimedia Foundation Inc.",vcard.get('org'));
	assertEquals('+1-727-231-0101',vcard.get('tel'));
	assertEquals('info@wikimedia.org',vcard.get('email'));
	assertEquals('USA',vcard.get('country-name'));
}
/* Disabled because of confirm alert. Can you simulate this in jsunit?


function testDestroy(){
	assertEquals(3,$RES('people').count());
	var person=$REC('person_3');
	person.destroy();
	assertNotNull(mock_request);
	assertEquals('/people/3',mock_request.url);
	assertEquals('delete',mock_request.options.method);
	assertNull(person.parentNode);
	assertEquals(2,$RES('people').count());
}
*/
</script>
</head>

<body>
	<h1>Unit tests for Resources</h1>
	<div id="people" class="people">
		<div id="person_1" class="person">
			<div class="name">Pelle</div>
			<div class="email">pelleb@gmail.com</div>
		</div>
		<div id="person_2" class="person">
			<div class="name">Bob</div>
			<div class="email">bob@gmail.com</div>
		</div>
		<div id="person_3" class="person">
			<div class="name">I will delete you</div>
			<div class="email">delete@gmail.com</div>
		</div>
		<div id="person_4" class="person">
			<div class="name fn">Change Me</div>
			<div class="email">change@gmail.com</div>
		</div>
		<div id="person_5" class="person">
			<div class="name">Change Me</div>
			<div class="email">change@gmail.com</div>
		</div>
	</div>
	<div id="team_1">
		<div class="name">The winning team</div>
	</div>
	<div id="message_1">
		<div class="body">We shall overcome</div>
		<abbr class="created_at" title="2007-05-01T16:45:07PDT">1 May 16:45</abbr>
	</div>
	<div id="message_2">
		<div class="body">We shall not fail</div>
		<abbr class="created_at">1 May 16:45</abbr>
	</div>
	
	<div id="contact_1" class="contact vcard">
	  <div class="fn org">Wikimedia Foundation Inc.</div>
	  <div class="adr">
	    <div class="street-address">200 2nd Ave. South #358</div>
	    <div>
	      <span class="locality">St. Petersburg</span>, 
	      <abbr class="region" title="Florida">FL</abbr> <span class="postal-code">33701-4313</span>
	    </div>
	    <div class="country-name">USA</div>
	    </div>
	  <div>Phone: <span class="tel">+1-727-231-0101</span></div>
	  <div>Email: <span class="email">info@wikimedia.org</span></div>
	  <div>
	    <span class="tel"><span class="type">Fax</span>: 
	    <span class="value">+1-727-258-0207</span></span>
	  </div>
	</div>
</body>
</html>